package com.ulic.customerManApplication.util.excel;

import java.io.File;
import java.io.FileInputStream;
import java.net.URLEncoder;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DownloadFile {

	private static final long serialVersionUID = 1533637970044170621L;

	public static void download(String realPath, String fileName,
			HttpServletRequest request, HttpServletResponse response) {
		FileInputStream inStream = null; // 输入流
		ServletOutputStream out = null; // 输出流
		File file = null;
		byte[] b = new byte[1024];
		int len = 0;
		String[] str = fileName.split(":");
		fileName = str[1];
		String realName = str[0];
 		file = new File(realPath + "/" + fileName);
		if (file.exists()) {
			try {
				inStream = new FileInputStream(file);
				response.reset();
				// response.setCharacterEncoding("utf-8");
				response.setContentType("application/x-download");// x-msdownload
				response.addHeader("Content-Disposition",
						"attachment;filename=\""
								+ URLEncoder.encode(realName, "utf-8") + "\"");
				out = response.getOutputStream();
				// 循环取出流中的数据

				while ((len = inStream.read(b)) > 0) {
					out.write(b, 0, len);
					response.flushBuffer();
				}
				inStream.close();

			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					out.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void downloadFile(String fileName,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		FileInputStream in = null; // 输入流
		ServletOutputStream out = null; // 输出流
		File file = null;
		try {
			String downloadName = fileName.substring(
					fileName.lastIndexOf("_") + 1, fileName.length());
			file = new File(fileName);
			if (!file.exists()) {
				request.setAttribute("message", "文件不存在!");
				request.getRequestDispatcher("/error.jsp").forward(request,
						response);
			}
			if (file.exists()) {
				in = new FileInputStream(fileName);
				if (request.getHeader("User-Agent").toLowerCase().indexOf(
						"firefox") > 0) {
					downloadName = new String(downloadName.getBytes("UTF-8"),
							"ISO-8859-1");// firefox浏览器
				} else if (request.getHeader("User-Agent").toUpperCase()
						.indexOf("MSIE") > 0) {
					downloadName = URLEncoder.encode(downloadName, "UTF-8");// IE浏览器
				} else {
					downloadName = URLEncoder.encode(downloadName, "UTF-8");// google浏览器
				}
				response.reset();
				response.setHeader("Content-Disposition",
						"attachment;filename=" + downloadName);
				response.setHeader("Connection", "close");
				response.setHeader("Content-Type", "application/octet-stream");
				out = response.getOutputStream();
				out.flush();
				int aRead = 0;
				while ((aRead = in.read()) != -1 & in != null) {
					out.write(aRead);
				}
				out.flush();
			}
		} catch (Throwable e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
				out.close();
			} catch (Throwable e) {
				e.printStackTrace();
			}
		}
	}
}
